package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.entity.find.FindMinesAndHots;
import com.hsq.androidproject.ui.find_fragment.taolun.ShowMineActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by lenovo on 2016/11/22.
 */

public class MyListViewFindMineAdapter extends BaseAdapter {        ////我的讨论区
    private List<FindMinesAndHots.MinesBean> mData;
    private Context context;


    public MyListViewFindMineAdapter(List<FindMinesAndHots.MinesBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_find_listview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.ll_layout = (LinearLayout) convertView.findViewById(R.id.ll_layout);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_dailyCount = (TextView) convertView.findViewById(R.id.tv_dailyCount);
            viewHolder.tv_lastTopicTitle = (TextView) convertView.findViewById(R.id.tv_lastTopicTitle);
            viewHolder.tv_lastTopicModify = (TextView) convertView.findViewById(R.id.tv_lastTopicModify);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final FindMinesAndHots.MinesBean minesBean = mData.get(position);
        final String thumb = minesBean.getThumb();
        Glide.with(context)
                .load(thumb)
                .into(viewHolder.iv_img);

        final String title = minesBean.getTitle();
        viewHolder.tv_name.setText(title);
        int dailyCount = minesBean.getDailyCount();
        if (dailyCount > 100) {
            viewHolder.tv_dailyCount.setText("99+");
        } else {
            viewHolder.tv_dailyCount.setText(dailyCount + "");
        }

        long lastTopicModify = minesBean.getLastTopicModify();
        String time = getTimeFromMillisecond(lastTopicModify);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new java.util.Date());

        long diff = 0;
        long hours = 0;
        long day = 0;
        long minutes = 0;
        try {
            Date d1 = sdf.parse(date);
            Date d2 = sdf.parse(time);
            diff = d1.getTime() - d2.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            day = days;
            hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (hours > 0) {///hours + ":" + minutes
            viewHolder.tv_lastTopicModify.setText(hours + "小时前");
        } else if (hours == 0) {
            if (minutes > 1) {
                viewHolder.tv_lastTopicModify.setText(minutes + "分钟前");
            } else {
                viewHolder.tv_lastTopicModify.setText("刚刚");
            }
        } else if (hours < 0 && hours > -23) {
            viewHolder.tv_lastTopicModify.setText("昨天");
        } else {
            viewHolder.tv_lastTopicModify.setText(day + "天前");
        }

        final String lastTopicTitle = minesBean.getLastTopicTitle();
        viewHolder.tv_lastTopicTitle.setText(lastTopicTitle);


        viewHolder.ll_layout.setOnClickListener(new View.OnClickListener() {        ///点击跳转页面
            @Override
            public void onClick(View v) {
                int bid = minesBean.getBid();
                Intent intent = new Intent(context, ShowMineActivity.class);        ////展示我的讨论区
                intent.putExtra("bid", bid);
                context.startActivity(intent);
            }
        });
        return convertView;


    }

    public static String getTimeFromMillisecond(Long millisecond) {		/*毫秒转换为时间*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date(millisecond);
        String timeStr = simpleDateFormat.format(date);
        return timeStr;
    }


    class ViewHolder {
        public ImageView iv_img;        //头像
        private TextView tv_name, tv_dailyCount, tv_lastTopicTitle, tv_lastTopicModify; //// 姓名 今日： 最后一个发表内容 时间
        private LinearLayout ll_layout;
    }


}
